package com.atguigu.web.controller.ai;

import com.atguigu.guli.ai.pojo.Chat;
import com.atguigu.guli.ai.pojo.Message;
import com.atguigu.guli.ai.service.AiService;
import com.atguigu.guli.ai.vo.ChatVo;
import com.atguigu.guli.ai.vo.MessageVo;
import com.atguigu.guli.ai.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("ai")
public class AiController {

    @Autowired
    private AiService aiService;

    /**
     * 创建会话
     * @return
     */
    @PostMapping("create-chat")
    public String createChat(@RequestBody ChatVo chatVo){
        String chatId = this.aiService.createChat(chatVo);
        return chatId;
    }

    @GetMapping("list-chat")
    public List<Chat> listChat(Long projectId, Long userId){
        List<Chat> chats = this.aiService.listChat(projectId, userId);
        return chats;
    }

    @PostMapping("update-chat")
    public String updateChat(@RequestBody ChatVo chatVo){
        this.aiService.updateChat(chatVo);
        return "更新成功！";
    }

    // 流式回答，并设置中文编码，防止出现乱码问题
    @PostMapping(value = "chat-stream", produces = "text/plain;charset=UTF-8")
    public Flux<String> chatStream(@RequestBody QueryVo queryVo){
        Flux<String> result = this.aiService.chatStream(queryVo);
        return result;
    }

    @PostMapping("save-msg")
    public String saveMsg(@RequestBody MessageVo messageVo){
        this.aiService.saveMsg(messageVo);
        return "保存回答成功";
    }

    @GetMapping("list-msg")
    public List<Message> listMsg(Long chatId){
        List<Message> msgs = this.aiService.listMsg(chatId);
        return msgs;
    }

}
